<?php
class HTML
{
	var $fileName;
	var $content;

	var $lg;

	var $match = "/\{([^}]+)\}/";

	//unidimension array holding variables found in the template
	var $internalVars; 

	//bidimension array holding variables sent through set_var
	var $externalVars;
	var $localCounter = 0;

	function __construct($fileName, $externalTemplate=false,$language='en')
	{
		if (!$externalTemplate)
			$this->filename = "HTML/".$fileName.".html";
		else
			$this->filename = "{$externalTemplate}/".$fileName;

		$this->lg = $language;

		$this->loadFile();
		$this->collectVariables();
	}


	function loadFile()
	{
		$this->content = file_get_contents($this->filename);
	}

	//replace meta variables
	

	function collectVariables()
	{
		preg_match_all($this->match, $this->content, $results);
		$this->internalVars = $results[0];
	}

	//sender can be internal => variable is not overwritten
	function set_var($var, $content, $sender='')
	{
		if ($sender == 'internal')
		{
			if (!isset($this->externalVars[$var]))
				$this->externalVars[$var] = $content;
		}
		else
			$this->externalVars[$var] = $content;
	}

	function eval_var($var, $with1='', $with2='', $with3='')
	{
		global $template_constants, $__auto_constants;
		if (!isset($template_constants[$var]))
			return "#";
		else
		{
			$base = $template_constants[$var];
			$text = str_replace("$1", $with1, $base);
			$text = str_replace("$2", $with2, $text);
			$text = str_replace("$3", $with3, $text);

			return $text;
		}
	}

	/* takes care of __auto variables such as __HTML_H */
	/* this is a time consuming, private method. Call only once, in the gen() -> meaning it will be executed for all subtemplates */
	private function postProcessing()
	{
		if (!is_array($this->internalVars))
			return "";

		global $__auto_constants;
		if (!is_array($__auto_constants))
			return "";
		//for all __constants
		foreach ($__auto_constants as $var_name => $content)
		{
			foreach ($this->internalVars as $inv_key => $inv_value)
			{
				if (strpos($inv_value, $var_name) !== false)
					$internalPosition = $inv_key;
				else
					continue;

				if ($internalPosition)
				{
					$use_var_name = str_replace("}","",str_replace("{","",$this->internalVars[$internalPosition]));
					list($na, $inVarName, $inVar1, $inVar2, $inVar3, $inVar4) = explode("__",$use_var_name);
					
					//find [] and evaluate if any
					$inVar1 = self::checkAndEvaluate($inVar1);
					$inVar2 = self::checkAndEvaluate($inVar2);
					$inVar3 = self::checkAndEvaluate($inVar3);
					$inVar4 = self::checkAndEvaluate($inVar4);
						
					$this->set_var($use_var_name, self::eval_var($var_name, $inVar1, $inVar2, $inVar3, $inVar4));
				}
			}
		}
	}


	private function checkAndEvaluate($varName)
	{
		if ($varName)
		{
			if ($varName[0] == '[')
			{
				$varName = str_replace("[","",str_replace("]","",$varName));
				return self::eval_var($varName);
			}
			else
				return $varName;
		}
		return $varName;
	}


	function gen()
	{
		//load template constants from main conf
		//Do this at the end so that Overriding a template_constant is possible
		global $template_constants;

		//load __auto templates
		$this->postProcessing();
		
		if(is_array($template_constants))
		{
			foreach ($template_constants as $key => $value)
			$this->set_var($key, $value, false);
		}
		
		if (!is_array($this->externalVars))
			return $this->content;
		else
		{
			foreach ($this->externalVars as $key => $value)
			{
				$prefix = $suffix = "";

				$placeHolders[] = "{".$key."}";

				if (strpos(strtolower($value), "px")=== false && (strpos($key, "__WIDTH")!== false || strpos($key, "__HEIGHT")!==false))
					$suffix = "px";

				$chainValues[] = $prefix.$value.$suffix;
			}

			$new_content = str_replace($placeHolders, $chainValues, $this->content);
		}

		return $new_content;
	}

	function __selectBox($width,$source, $objName, $onChangeEvt, $selected, $firstElem='', $onClickEvt='', $type='')
	{
		if ($type)
			$template = new HTML("__selectbox_$type");
		else
			$template = new HTML("__selectbox");

		$template -> set_var("NAME_ID", "id = '$objName' name='$objName' ");
		$template -> set_var("ONCHANGE_EVT" ,"$onChangeEvt");
		$template -> set_var("ONCLICK_EVT", $onClickEvt);
		$template -> set_var("WIDTH","$width");
		if (!is_array($source))
		{
			$option = new HTML("__selectbox.options");
			$option -> set_var("VALUE", "-1");
			$option -> set_var("LABEL", "$source");
			$template -> set_var("OPTIONS", $option->gen());

			return $template -> gen();
		}

		$content = "";
		//first element
		$option = new HTML ("__selectbox.options");
		$option -> set_var("VALUE", -1);
		if ($firstElem !== '')
			$option -> set_var("LABEL", $firstElem);
		else
			$option -> set_var("LABEL", "Choose from the list");
		$option -> set_var("SELECTED_STATE", "");
		$content .= $option -> gen();


		//all the other elements
		foreach ($source as $key => $value)
		{
			$option = new HTML("__selectbox.options");
			$option -> set_var("VALUE", "$key");
			$option -> set_var("LABEL", "$value");
			if ($key == $selected)
				$option -> set_var("SELECTED_STATE", " selected ");
			else
				$option -> set_var("SELECTED_STATE", "");
			$content .= $option -> gen();
		}

		$template -> set_var("OPTIONS", $content);
		return $template -> gen();
	}
	
}


?>
